package cc.rengu.igas.route.core.realize.payment.impl.upac;

import cc.rengu.igas.channel.upac.biz.impl.UpacCollectionServiceImpl;
import cc.rengu.igas.channel.upac.facade.base.Header;
import cc.rengu.igas.channel.upac.facade.bean.CustomerInfo;
import cc.rengu.igas.channel.upac.facade.request.CollectionSignRequest;
import cc.rengu.igas.channel.upac.facade.response.CollectionSignResponse;
import cc.rengu.igas.channel.upac.facade.result.Result;
import cc.rengu.igas.route.core.realize.payment.DstChannelPaymentSignVerifyService;
import cc.rengu.igas.route.facade.bean.PayerInfo;
import cc.rengu.igas.route.facade.request.IntelligencePaymentSignVerifyRequest;
import cc.rengu.igas.route.facade.response.IntelligencePaymentSignVerifyResponse;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 全渠道对外收款签约确认实现
 *
 * @author lizhangtao
 * @version 1.0.0
 * @date 2020-04-30
 */
public class UpacPaymentSignVerifyServiceImpl implements DstChannelPaymentSignVerifyService {
    private final static String TXN_NUM = "7216000000";
    /**
     * 默认渠道类型 07-互联网
     */
    private final static String DEFAULT_CHANNEL_TYPE = "07";
    /**
     * 通道类型为代收CT  商户选择其开通的产品时取自支付通道信息表T_P_DST_CHANNEL_CFG
     */
    private final static String DST_CHANNEL_TYPE = "CT01";
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public IntelligencePaymentSignVerifyResponse paymentSignVerify(IntelligencePaymentSignVerifyRequest signVerifyRequest, PayerInfo payerInfo, String origSysSeqNum) throws Exception {

        IntelligencePaymentSignVerifyResponse signVerifyResponse = new IntelligencePaymentSignVerifyResponse();
        signVerifyResponse.setHeader(signVerifyRequest.getHeader());
        signVerifyResponse.setDstChannelId("UPAC");
        signVerifyResponse.setMchntNo(signVerifyRequest.getMchntNo());
        //设置请求值
        CollectionSignRequest collectionSignRequest = new CollectionSignRequest();
        collectionSignRequest.setProdType(DST_CHANNEL_TYPE);
        collectionSignRequest.setChannelType(DEFAULT_CHANNEL_TYPE);
        collectionSignRequest.setPayAcctNo(payerInfo.getAcctNo());
        collectionSignRequest.setOrderId(signVerifyRequest.getHeader().getTraceNo());
        collectionSignRequest.setOrderTime(signVerifyRequest.getHeader().getTransDate() + signVerifyRequest.getHeader().getTransTime());
        collectionSignRequest.setBillType("OT");
        collectionSignRequest.setBillNo(payerInfo.getCertId());
        //接口代收频率，若不送则默认000000不限次数
        collectionSignRequest.setFrequency("000000");
        //委托期限，若不送则使用999个月
        collectionSignRequest.setValidMonth("999");
        //短信验证码送上去
        CustomerInfo customerInfo = new CustomerInfo();
        customerInfo.setAuthCode(signVerifyRequest.getSmsCode());
        customerInfo.setCustName(payerInfo.getAcctName());
        customerInfo.setPhoneNo(payerInfo.getPhoneNo());
        customerInfo.setCertNo(payerInfo.getCertId());
        customerInfo.setCertType("01");
        collectionSignRequest.setCustomerInfo(customerInfo);
        customerInfo.setAuthCode(signVerifyRequest.getSmsCode());
        //设置请求头
        Header header = new Header();
        BeanUtil.beanCopy(signVerifyRequest.getHeader(), header);
        header.setTxnNum(TXN_NUM);
        collectionSignRequest.setHeader(header);
        //调用通道进行签约确认
        DubboService dubboService = new DubboServiceImpl();
        Result<CollectionSignResponse> collectionSignResponseResult = (Result<CollectionSignResponse>) dubboService.callDubboInnerMethod(UpacCollectionServiceImpl.class, "collectionSign", collectionSignRequest);
        if (!collectionSignResponseResult.isSuccess()) {
            throw new BizException(collectionSignResponseResult.getResult().getRespCode(), collectionSignResponseResult.getResult().getRespDesc());
        }
        if (null == collectionSignResponseResult.getResult()) {
            throw new BizException(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode(), OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
        }
        signVerifyResponse.setRespCode(collectionSignResponseResult.getResult().getRespCode());
        signVerifyResponse.setRespDesc(collectionSignResponseResult.getResult().getRespDesc());

        return signVerifyResponse;
    }
}
